package org.skyeye.modeling.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.skyeye.modeling.config.AppConstant;
import org.skyeye.modeling.model.MDevice;
/**
 * @author Administrator
 *
 */
public class DeviceFigure extends Shape {

	private Label label;
	private MDevice model = null;

	public DeviceFigure(MDevice m) {
		this.label = new Label();
		model = m;
		label.setText(model.getName());
		label.setIconTextGap(5);
		label.setTextAlignment(Label.CENTER);
		this.add(label);
	}

	public String getText() {
		return this.label.getText();
	}

	public Rectangle getTextBounds() {
		return this.label.getTextBounds();
	}

	public void setName(String name) {
		this.label.setText(name);
		this.repaint();
	}

	public Point getLeftAnchorLoc() {
		Point rst = Point.SINGLETON;
		rst.x = this.getBounds().x;
		rst.y = this.getBounds().y + this.getBounds().height / 2;
		return rst;
	}

	public Point getRightAnchorLoc() {
		Point rst = Point.SINGLETON;
		rst.x = this.getBounds().x + this.getBounds().width;
		rst.y = this.getBounds().y + this.getBounds().height / 2;
		return rst;
	}

	public Point getTopAnchorLor() {
		Point rst = Point.SINGLETON;
		rst.x = this.getBounds().x + this.getBounds().width / 2;
		rst.y = this.getBounds().y;
		return rst;
	}

	public Point getBottomAnchorLor() {
		Point rst = Point.SINGLETON;
		rst.x = this.getBounds().x + this.getBounds().width / 2;
		rst.y = this.getBounds().y + this.getBounds().height;
		return rst;
	}

	/**
	 * @see Shape#fillShape(Graphics)
	 */
	protected void fillShape(Graphics graphics) {
		graphics.pushState();
		Rectangle bound = this.getBounds().getCopy();
		Color backgroundColor = ColorConstants.orange;
		if (model != null) {
			if (model.getBaseType().equals(AppConstant.BASE_LINKER)) {
				backgroundColor = AppConstant.LINKER_BACKGROUND_COLOR;
			} else if (model.getBaseType().equals(AppConstant.BASE_CPU)) {
				backgroundColor = AppConstant.CPU_BACKGROUND_COLOR;
			}
		}
		graphics.setBackgroundColor(backgroundColor);
		graphics.setForegroundColor(backgroundColor);
		graphics.fillGradient(bound, false);
		graphics.drawRoundRectangle(bound, 0, 0);
		graphics.popState();
	}
	/**
	 * @see Shape#outlineShape(Graphics)
	 */
	protected void outlineShape(Graphics graphics) {
		graphics.pushState();
		Rectangle bound = this.getBounds().getCopy();
		graphics.setLineStyle(SWT.BOLD);
		graphics.setLineWidth(4);
		graphics.drawRoundRectangle(bound, 0, 0);
		graphics.popState();
	}

	public void setBounds(Rectangle rect) {
		super.setBounds(rect);
		Rectangle tmp = new Rectangle(rect.x + 2, rect.y + 2, rect.width, 30);
		tmp.height = 25;
		this.label.setBounds(tmp);
	}
}